package com.mapbased.sfw.util;

import java.io.File;
import java.io.StringReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.mapbased.sfw.model.LoadContext;
import com.mapbased.sfw.util.logging.ESLogger;
import com.mapbased.sfw.util.logging.Loggers;

public class MimeMapping {

	static ESLogger log=Loggers.getLogger(MimeMapping.class);
	public static Map<String,String> mimes;
	
	static{
	try {
		load();
	} catch (DocumentException e) {
	 
		log.error("Cannot parse mime mapping file",e);
	}	
	}
	static public void load() throws DocumentException{
		SAXReader reader = new SAXReader();
		reader
				.setEntityResolver(com.mapbased.sfw.util.WfwEntityResolver.INSTANCE);

		File f=new File( "../conf/web.xml");
		if(!f.exists()){
			log.error("Cannot find mime mapping file:{}", f.getAbsolutePath());
		}
		Document doc = reader.read( f);

		Element root = doc.getRootElement();
		
		List l=root.elements("mime-mapping");
		 Map<String,String> m=new HashMap();
		for(Object o:l){
			Element e=(Element)o;
			try{
			String k=e.element("extension").getText().trim();
			String v=e.element("mime-type").getText().trim();
			m.put(k, v);
			}catch(Exception ee){
				log.warn("Xml format:{}" , e.toString());
			}
			
			 
		}
		mimes=m;

	 
	}
	
	public static void main(String [] ss){
	 System.out.println(mimes );
	}
}
